#!/bin/bash 

#  biduleur_intall.sh
#  Wed Feb 10 19:37:18 2010
#  Copyright  2010  mk67
#  <rmk677@gmail.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA

SHELL=/bin/bash
PATH=/usr/bin:/bin:/usr/local/bin

 cd /
### VARS
### -------------------------------------------------------
## Script colours Vars.
t='\e[0;39m'	#default terminal colour
r='\e[1;31m'	#red
v='\e[1;32m'	#green
y='\e[1;36m'	#cyan
j='\e[1;33m'	#yellow

## error messages
eA="\n$r  Error, files not copied properly.\n  Abort."
eB="\n$r  Error, files not configured properly.\n  Abort."
eC=
eD=

### MAIN FUNCTIONS
ID(){
## card id 
board=$(lspci -nn | grep 'VGA' | awk '{ print $9" "$10" "$11" "$12" "$13 }' | tr '[[]]' ' ')
## vendor
vendor=$(lspci -nn | grep 'VGA' | awk '{ print  $1" "$6" "$7 }')
}
## system arch
arch(){
#arch=$(uname -a | awk '{ print $12 }') == "i686"
if [ $(uname -a | awk '{ print $12 }') == "i686" ] || [ $(uname -a | awk '{ print $12 }') == "i586" ]; then
	ARCH="x86"
else
	ARCH="x86_64"
fi
}
## system distro --> need progress
function distro(){
if [ -f /proc/version ] ; then
	version="cat /proc/version"
	cat /proc/version | grep ubuntu >/dev/null
	if [ $? == 0 ]; then
	DISTRO="UBUNTU"
	PKG_INSTALLER="apt-get install"
	version=$( uname -r | cut -d '-' -f 1 )
	PKG="lftp kernel-headers-$version gcc xterm"
	fi
	cat /proc/version | grep 'mandriva' >/dev/null
	if [ $? == 0 ]; then
	DISTRO="MANDRIVA"
	PKG_INSTALLER="urpmi"
	desk=$( uname -r | cut -d '-' -f 2 )
	kernel=$( uname -r | cut -d '-' -f 1 )
	version=$( uname -r | cut -d '-' -f 3 )
	PKG="lftp kernel-$desk-devel-$kernel-$version gcc xterm"
	fi
else
	echo -e "\f$r Distro not in list. Install dependencies manually "
fi
}
# elf type
function libclass(){
	if [[ $(uname -a | awk '{ print $15 }') == 64 ]] ; then
		ELF_TYPE="64"
	else
		ELF_TYPE=""	
	fi
}
function version_id(){
	libclass
	version=$(echo /usr/lib$ELF_TYPE/xorg/modules/extensions/libglx.so.*)
	echo ${version#*so.}
}
# NVIDIA directory creation
create_dir(){
	echo -e "$v  Creation of the Working directories.\n  Main script is automatically install in /usr/local/bin."
	echo -e "$v  Choose the directory you want with [TAB] and completion.\n  I prefer /usr/local/src/"
	echo -e "$v  but it is really arbitrary:\n"
	echo -e "$y  ==>$j \c"
	read -e source_dir
	mkdir -p $source_dir/NVIDIA
	nvdir="$source_dir/NVIDIA"
	echo -e "\f$r**$y  PERFORMING INSTALL IN $j$nvdir$r  **$v\f"
}
# Internet connection control function.
function ifnet(){ 
		if [ -n /sbin/ifconfig ] ; then
			if [[ "$( /sbin/ifconfig | grep 'ppp0' | awk '{print $1}' )" != "ppp0" ]] ; then
				net="ppp0"
			elif [[ "$( /sbin/ifconfig | grep 'eth0' | awk '{print $1}' )" != "eth0" ]] ; then
				net="eth0"
			elif [[ "$( /sbin/ifconfig | grep 'eth1' | awk '{print $1}' )" != "eth1" ]] ; then
				net="eth1"
			elif [[ "$( /sbin/ifconfig | grep 'wlan0' | awk '{print $1}' )" != "wlan0" ]] ; then
				net="wlan0"
			fi
		fi
		/sbin/ifconfig $net | awk '{print $2}' | sed -n '2p'
}
# dependencies control
function dep_install(){
	if [ "$( ls /usr/bin/lftp )" != "" ] ; then
		echo -e "$v\f  Dependencies are already present. Skipping... "
	else
		if [ "$( ls /usr/bin/lftp )" == "" ] ; then 
			if [[ "`ifnet`" == "adr:127.*" ]] ; then
				echo -e "\f$v I need to be connected\n to the Net to download package\f Connect me and restart the script.$t"
				exit 0
			else
				if [[ "`ifnet`" != "adr:127.*" ]] ; then
					echo -e "$r  Required dependencies are not installed."; sleep 2
					echo -e "$y  MISSING PACKAGE INSTALL RUNNING ...\n"
					echo -e "$v  Packager : $j$PKG_INSTALLER" 
					echo -e "$v  Package(s) list to install :\n  $j$PKG$y\f"
					$PKG_INSTALLER $PKG
					if [ -x /usr/bin/lftp ] && [ -x /usr/bin/perl ] ; then 
						echo -e "$r**$y  INSTALL DONE$r  **\f "
					else 
						echo -e "$r  Error, package not installed, proceed manually\n  After this the end of this script.\f$v"
					fi
				fi
			fi
		fi
	fi
}

# sub directories check
sub_dir(){
if [ "$( ls $nvdir/ )" == "" ] ; then
	## dir vars.
	nvtar="$nvdir/tgz"					# archives directory
	nvtmp="$nvdir/temp"					# install temp directory
	nvlog="$nvdir/log"					# logs directory
	nvdl="$nvdir/release"				# downlaod driver backups directory
	nvupdate="$nvdir/update"			# update temp directory
	echo -e "$y\f  The$j NVIDIA MAKER$y sub-directories don't exist\n  I create them ...\f"
	mkdir -p $nvtar $nvtmp $nvlog $nvupdate $nvdl
	#ls -l $nvdir
else
	if [ "$( ls $nvdir/ )" != "" ] ; then 
	echo -e "\f$j  $nvdir$v sub-directories are present, Skipping ..." #; ls $nvdir
	fi
fi
}
# sub script install
sub_script(){
# xsetup & tabatha install & config control
#ls $nvdir/tabatha_script > /dev/null
cd $( find /home/$USER -name 'nvidia-maker-installer' )
if [ '$( ls $nvdir/tabatha_vidia )' != '' ] || [ '$( ls $nvdir/xsetup )' != '' ]; then 
	echo -e "\n$v  NVIDIA xsetup.sh & Xorg.conf UI are not installed yet."; sleep 2
	good=" properly" ; bad="$r is not"
	if [ -x xsetup ] || [ -x tabatha_vidia ] ; then
		echo -e "$y  Proceed now.\n  Install starts ...\f"
		rm -f /usr/local/bin/nvidia-maker-*.sh
		cp -f nvidia-maker-*.sh $nvdir
		cp -f nvidia-biduleur-Xterm-UI-all.sh /usr/local/bin/ 
		ln -s -f $nvdir/nvidia-maker-*.sh /usr/local/bin/
		cp -f xsetup.sh $nvdir
		cp -f tabatha_vidia $nvdir
		ln -s -f $nvdir/xsetup.sh /usr/local/bin/
		ln -s -f $nvdir/tabatha_vidia /usr/local/bin/
		cp -Rf xorg_conf $nvdir
		cp -Rf etc.nv $nvdir
		cp -f arts/*.png /usr/local/share/pixmaps
		cp -f arts/*.desktop /usr/local/share/applications
		if [ -x $nvdir/xsetup ] || [ -x $nvdir/tabatha_vidia ] ; then
			echo -e "$r**$y  INSTALL DONE$r  **\f "
			echo -e "$v  Linking configuration files to the system.\n$y  Config starts ...\f"
			#for dest in $query ; do
			nb(){
				cat -n $nvdir/$dest | grep "$query" | awk '{ print $1 }'
			}
			#field="tabatha_vidia":"xsetup.sh":"videomod.conf":"script.xml"
			#n="$( cat -n $nvdir/$dest | grep "$query" | awk '{ print $1 }')"
			if  [ -s $nvdir/nvidia-maker-*.sh ] ; then
				query="## Master Vars."
				dest="nvidia-maker-*.sh"
				n=$(nb) #$(cat -n $nvdir/$dest | grep "$query" | awk '{ print $1 }')
				sed -i "`echo $(( $n+1 ))`cnvdir=\"$nvdir\"" $nvdir/$dest
				if [ "$( ls -l $nvdir/$dest | awk '{ print $5 }' )" != "$( ls -l $dest | awk '{ print $5 }' )" ] ; then
					msg=$good
				else
					msg=$bad
				fi
				echo -e "$j  $dest$y\t>> $msg$y configured.$y"
				if [ "$msg" == "$bad" ] ; then
					echo -e "$eB$t\f" ; exit 0
				fi
			fi
			if  [ -s $nvdir/tabatha_vidia ] ; then
				query="# Main exec" #"if (-e \'"
				dest="tabatha_vidia"
				n=$(nb) #$(cat -n $nvdir/$dest | grep "$query" | awk '{ print $1 }')
				#cat $nvdir/$dest | grep "$query" | sed -i 's/'$nvdir'/'$nvdir'/etc.nv\/script.xml\' $nvdir/$det
				sed -i "`echo $(( $n+1 ))`cif (-e \'$nvdir\/etc.nv\/script.xml\') {" $nvdir/$dest
				sed -i "`echo $(( $n+2 ))`c    \$config = \"$nvdir\/etc.nv\/script.xml\"" $nvdir/$dest
				if [ "$( ls -l $nvdir/$dest | awk '{ print $5 }' )" != "$( ls -l $dest | awk '{ print $5 }' )" ] ; then
					msg=$good
				else
					msg=$bad
				fi
				echo -e "$j  $dest$y\t\t>> $msg$y configured.$y"
				if [ "$msg" == "$bad" ] ; then
					echo -e "$eB$t\f" ; exit 0
				fi
			fi
			if  [ -s $nvdir/xsetup.sh ] ; then
				query="#! \/bin\/bash"
				dest="xsetup.sh"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c\. $nvdir\/etc.nv\/videomode.conf" $nvdir/$dest
				if [ "$( ls -l $nvdir/$dest | awk '{ print $5 }' )" != "$( ls -l $dest | awk '{ print $5 }' )" ] ; then
					msg=$good
				else
					msg=$bad
				fi
				echo -e "$j  $dest$y\t\t>> $msg$y configured.$y"
				if [ "$msg" == "$bad" ] ; then
					echo -e "$eB$t\f" ; exit 0
				fi
			fi
			if  [ -s $nvdir/etc.nv/videomode.conf ] ; then
				query="#nvidia xsetup"
				dest="etc.nv/videomode.conf"
				n=$(nb)
				sed -i "`echo $(( $n+7 ))`cdir=\"$nvdir\/etc.nv\/xorg_conf\"" $nvdir/$dest
				if [ "$( ls -l $nvdir/$dest | awk '{ print $5 }' )" != "$( ls -l $dest | awk '{ print $5 }' )" ] ; then
					msg=$good
				else
					msg=$bad
				fi
				echo -e "$j  ${dest#*/}$y\t>> $msg$y configured.$y"
				if [ "$msg" == "$bad" ] ; then
					echo -e "$eB$t\f" ; exit 0
				fi
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"Dual\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu $nvdir\/xsetup dual\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"TwinView\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu $nvdir\/xsetup twin\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"Single\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu $nvdir\/xsetup dfp\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"External\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu $nvdir\/xsetup crt\<\/command\>" $nvdir/$dest
			fi
			## xorg config link part
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"Single Config\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu gedit $nvdir\/xorg_conf\/xorg.dfp\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"External Config\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu gedit $nvdir\/xorg_conf\/xorg.crt\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"TwinView Config\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu gedit $nvdir\/xorg_conf\/xorg.twin\<\/command\>" $nvdir/$dest
			fi
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"Dual Config\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu gedit $nvdir\/xorg_conf\/xorg.dual\<\/command\>" $nvdir/$dest
			fi
			## tabatha xml part
			if  [ -s $nvdir/etc.nv/script.xml ] ; then
				query="title=\"Xml Config\""
				dest="etc.nv/script.xml"
				n=$(nb)
				sed -i "`echo $(( $n+1 ))`c    \<command\>gnomesu $nvdir\/etc.nv\/script.xml\<\/command\>" $nvdir/$dest
				if [ "$( ls -l $nvdir/$dest | awk '{ print $5 }' )" != "$( ls -l $dest | awk '{ print $5 }' )" ] ; then
					msg=$good
				else
					msg=$bad
				fi
				echo -e "$j  ${dest#*/}$y\t\t>> $msg$y configured.$y\n"
				if [ "$msg" == "$bad" ] ; then
					echo -e "$eB$t\f" ; exit 0
				fi
			fi
			## xorg configuration part 
		else
			echo -e "$eA$t\f"
			exit 0
		fi		
	else
		echo -e "\n$r  Error, files not found.\n  Abort."
		echo -e "\n$y  PLEASE, CHECK THE TARBALL & RUN INSTALL SCRIPT AGAIN. $t\f"
		exit 0
	fi
echo -e "$r**$y  ALL CONFIGS DONE$r  **\f "
fi
}
### FIRST START
INTRO(){
echo -e "\f\t$j--- WELCOME TO THE NVIDIA \"BIDULEUR\" INSTALLER ---         \f" ; sleep 1
echo -e "\f$v  This script is going to install the BIDULEUR into you system,\r"
echo -e "  Answer to questions and fill the necessary when prompt \f"
## Creation of necessary directories in case they arent already there.:
distro
echo -e "$v  Distro :\t\t $j$DISTRO$v\n  Processeur type :\t $j$ARCH$v \f"
echo -e "$v  What do you want to do with the Nvidia Swiss Knife? :\n$y"
a0="Install" ; a1="Remove" ; a2="Quit Installer"
select M1 in "$a0" "$a1" "$a2" ; do
	case $M1 in
		"$a0")
			c1="/usr" ; c2="/opt" ; c3="/home"
			find $c1 $c2 $c3 -name NVIDIA | cat - > /tmp/NV1
			if [ "$(find $c1 $c2 $c3 -name NVIDIA )" != "" ] ; then
				echo -e "\f$v  Nvidia Bidule Maker folders already exist. "
				if [ "$( cat -n /tmp/NV1 | awk '{ print $1 }' | sed -n '2p' )" >> "1" ] ; then
					echo -e "$v  But I found more than one directory on your system:\n"
				else
					echo -e "$v  I found them in $j$( cat /tmp/NV1 | awk '{ print $1 }' )$v.\n"
				fi
				echo -e "$v  Did I still use them or make install in a new one?$y"
				b0="Old" ; b1="New" ; b2="Quit installer"
				select M2 in "$b0" "$b1" "$b2"; do
					case $M2 in
						"$b0")
							if [ "$( cat -n /tmp/NV1 | awk '{ print $1 }' | sed -n '2p' )" >> "1" ] ; then
								cat -n /tmp/NV1 ; echo -e "\r$v Your Choice :\n ==>  $j\c"
								read nb
								nvdir=$( cat -n /tmp/NV1 | awk '{ print $2 }' | sed -n `echo $nb`p )
							else
								nvdir=$( cat /tmp/NV1 )
							fi
							echo -e "\n$v  Making install in $j$nvdir.\n"
							dep_install
							sub_dir
							sub_script
							echo -e "$v  If this message appears, it's probably that's all thinks rock!\n  Have a nice day!\f$t"
							rm -f /tmp/NV*
							exit 0
						;;
						"$b1")
							create_dir
							dep_install
							sub_dir
							sub_script
							echo -e "$v  If this message appears, it's probably that's all thinks rock!\n  Have a nice day!\f$t"
							exit 0
						;;
						"$b2")
							exit 0
						;;
					esac
				done
			fi
		;;
		"$a1")
			rm -f /usr/local/bin/nvidia-maker-*.sh
			rm -f /usr/local/bin/nvidia-maker-Xterm-UI-all.sh
			rm -f /usr/local/bin/tabatha_vidia
			rm -f /usr/local/share/applications/nvidia-biduleur*.desktop
			rm -f //usr/local/share/pixmaps/the-nvidia-swiss_knife.png
			rm -f /etc/X11/xorg.conf
			cp -f $nvdir/xorg.orig /etc/X11/xorg.conf
			rm -fR $found_dir
			exit 0
		;;
		"$a2")
			echo -e "$t \f"
			exit 0
		;;
	esac
done
}
### SCRIPT INTRO
case "$1" in
	-x|--xterm_ui)
		#echo "$0"
		xterm -sb -b 5 -bg black -bd black -bw 0 -geometry 70x24 -e /home/$USER/$0 XC
		#xterm -e /home/$USER/$0 XC
	;;
	-t|--terminal)
		ID ; arch ; INTRO
	;;
	XC)
		ID ; arch ; INTRO
	;;
	-h|--help)
		echo -e "nvidia_maker_install.sh usage :\n\t -t, --terminal \t exec intaller in terminal mode."
		echo -e "\t -x, --xterm_ui \t exec in a pretty xterm interface. \n\t -h, --help \t\t this help."
		exit 1
	;;
	*)
		exit 0
	;;
esac
exit 0